//
//  CommEditViewController.m
//  NewIMGII
//
//  Created by 步遴 李 on 12-7-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CommEditViewController.h"
#import "AFNetworking.h"
@implementation CommEditViewController
@synthesize blogid,textview,operationQueue;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    UIBarButtonItem* navButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发送"  
                                                                     style:UIBarButtonItemStylePlain 
                                                                    target:self 
                                                                    action:@selector(SendComment)];
    self.navigationItem.rightBarButtonItem = navButtonItem;
    self.operationQueue = [[NSOperationQueue alloc] init];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)SendComment{
    NSString * comName = [[NSString alloc]initWithFormat:@"iphone网友"];
    NSString * postCoent = self.textview.text;
    if (postCoent.length < 2) {
        return;
    }

    NSString *postUrl = [[NSString alloc]initWithFormat:@"http://www.imgii.com/?wpapi=comment&name=%@&email=iphone@apple.com&content=%@&post_id=%@",comName,postCoent,self.blogid];
    postUrl = [postUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:postUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        [self.navigationController popViewControllerAnimated:YES];
    }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
        [self.navigationController popViewControllerAnimated:YES];
    }];    
    [self.operationQueue addOperation:operation];                                     
    return;
}

@end
